//
//  GQHRouteNewController.m
//  Xanthium
//
//  Created by GuanQinghao on 16/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHRouteNewView.h"

#pragma mark Controller
#import "GQHRouteNewController.h"
#import "GQHRegionController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHRouteNewController () <GQHRouteNewViewDelegate> {
    
#pragma mark Data
    __block NSMutableDictionary *parameters;
#pragma mark View
    /// 自定义根视图
    GQHRouteNewView *rootView;
}

@end


@implementation GQHRouteNewController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHRouteNewView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"添加路线";
    [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
            
        case 0: {
            
            /// 起始地
            GQHRegionController *regionController = [[GQHRegionController alloc] init];
            regionController.qh_regionBlock = ^(NSString *regionID, NSString *regionName, CLLocation *location)
            {
                
                [parameters setObject:regionID forKey:@"originId"];
                [rootView.qh_contentArray qh_safetySetObject:regionName atIndex:0];
                [rootView.qh_tableView reloadData];
            };
            [self.navigationController pushViewController:regionController animated:YES];
            
            DLog(@"%@",parameters);
        }
            break;
        case 1: {
            
            /// 目的地
            GQHRegionController *regionController = [[GQHRegionController alloc] init];
            regionController.qh_regionBlock = ^(NSString *regionID, NSString *regionName, CLLocation *location)
            {
                
                [parameters setObject:regionID forKey:@"destinationId"];
                [rootView.qh_contentArray qh_safetySetObject:regionName atIndex:1];
                [rootView.qh_tableView reloadData];
            };
            [self.navigationController pushViewController:regionController animated:YES];
            
            DLog(@"%@",parameters);
        }
            break;
        default:
            break;
    }
}
#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buFocusOnLine/1/add"];
    
    [parameters setObject:USER_GET_ID forKey:@"token"];
    
    if (parameters.count < 3) {
        
        [self showToastWithText:@"补全路线信息"];
        return;
    }
    
    [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        
        if (0 == [code intValue]) {
            
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
    }];
}

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
